package light.mvc.controller.hrs.front;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import light.mvc.controller.base.BaseController;
import light.mvc.framework.constant.GlobalConstant;
import light.mvc.pageModel.base.SessionInfo;
import light.mvc.pageModel.hrs.HRSExpert;
import light.mvc.service.hrs.HRSDataServiceI;
import light.mvc.service.hrs.HRSExpertServiceI;

@Controller
@RequestMapping("/front/hrsexpert")
public class HRSExpertFrontController extends BaseController{
	@Autowired
	private HRSExpertServiceI expertService;
	
	@Autowired
	private HRSDataServiceI dataService;
	
	@RequestMapping("/register")
	@ResponseBody
	public HashMap<String,String>register(HRSExpert expert,HttpSession session){
		HashMap<String,String> res=new HashMap<String,String>();
		HRSExpert u=expertService.getByLoginName(expert);
		if(u!=null){
			res.put("result", "fail");
			res.put("tip", "该号码已注册");
		}
		else{
			expertService.add(expert);
			res.put("result", "success");
			res.put("tip", "注册成功");
			SessionInfo sessionInfo=new SessionInfo();
			sessionInfo.setRoleType(GlobalConstant.EXPERT);
			
			HRSExpert registedExpert=expertService.getByLoginName(expert);
			sessionInfo.setId(registedExpert.getId());
			sessionInfo.setSessionId(session.getId());
			sessionInfo.setLoginname(registedExpert.getTel());
			sessionInfo.setName(registedExpert.getName());
			session.setAttribute(GlobalConstant.SESSION_INFO_FRONT_EXPERT, sessionInfo);
			
		}
		
		return res;
	}
	@RequestMapping("/login")
	@ResponseBody
	public HashMap<String,Object> login(HRSExpert expert, HttpSession session){
		HashMap<String,Object> res=new HashMap<String,Object>();
		
		//
		HRSExpert checkExpert=expertService.getByLoginName(expert);
		if(checkExpert==null){
			res.put("result", "false");
			res.put("tip", "该号码未注册");
			return res;
		}
		
		
		HRSExpert loginExpert=expertService.login(expert);
		if(loginExpert!=null){
			res.put("result", "success");
			SessionInfo sessionInfo=new SessionInfo();
			sessionInfo.setRoleType(GlobalConstant.EXPERT);
			sessionInfo.setId(loginExpert.getId());
			sessionInfo.setSessionId(session.getId());
			sessionInfo.setLoginname(loginExpert.getTel());
			sessionInfo.setName(loginExpert.getName());
			
			session.setAttribute(GlobalConstant.SESSION_INFO_FRONT_EXPERT, sessionInfo);
			res.put("sessionid", session.getId());
		}
		else{
			res.put("result", "fail");
			res.put("tip", "号码或密码错误");
		}
		return res;
	}
}
